package com.ljx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljx.R;
import com.ljx.pojo.CrmBanner;
import com.ljx.service.impl.CrmBannerServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mybatis.spring.annotation.MapperScan;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author 李加喜
 * @since 2020-09-28
 */
//管理员控制的方法
@Api(description = "Banner操作")
@RestController
@RequestMapping("/servicecms/banner/admin")
@CrossOrigin
@MapperScan("com.ljx.mapper")
public class BannerAdminController {
    @Autowired
    private CrmBannerServiceImpl crmBannerAdminService;

    //实现对banner的增加
    @ApiOperation("Banner的添加")
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody(required = false) CrmBanner crmBanner){
        crmBannerAdminService.save(crmBanner);
        return R.ok();
    }
    //实现对Banner的删除操作
    @ApiOperation("Banner的删除")
    @DeleteMapping("/delBanner/{bannerId}")
    public R delBanner(@PathVariable String bannerId){
        crmBannerAdminService.removeById(bannerId);
        return R.ok();
    }
    //分页查询Banner的信息
    @ApiOperation("分页查询Banner")
    @GetMapping("/selBanner/{current}/{limit}")
    public R selBanner(@PathVariable Long current,
                       @PathVariable Long limit){
        Page<CrmBanner> page = new Page<CrmBanner>(current, limit);
        crmBannerAdminService.page(page,null);
        List<CrmBanner> list = page.getRecords(); //查询所有的集合
        long total = page.getTotal(); //查询出来的记录数
        return R.ok().data("list",list).data("total",total);
    }
    //根据id查询Banner的信息
    @GetMapping("/getBanner/{bannerId}")
    public R getBannerId(@PathVariable String bannerId){
        CrmBanner crmBanner = crmBannerAdminService.getById(bannerId);
        return R.ok().data("crmBanner",crmBanner);
    }
    //修改Banner信息
    @ApiOperation("修改Banner的信息")
    @PostMapping("/upadte")
    public R updBanner(@RequestBody(required = false) CrmBanner crmBanner){
        boolean flag = crmBannerAdminService.updateById(crmBanner);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

